package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.Category;
import com.woniu.dao.CategoryDao;
import com.woniu.entity.dto.CategoryDTO;
import com.woniu.entity.Course;
import com.woniu.entity.PageResult;
import com.woniu.entity.vo.CategoryVO;
import com.woniu.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * (Category)表服务实现类
 *
 * @author makejava
 * @since 2025-06-10 19:03:23
 */
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryDao categoryDao;
    /**
     * 获取全部课程分类信息
     * @return
     */
    @Override
    public List<CategoryVO> getCategoryAll() {
        List<CategoryVO> categoryVOList = categoryDao.getCategoryAll();
        return setChildrenCategoryVO(categoryVOList,0);
    }

    /**
     * 添加课程分类信息
     * @param category
     */
    @Override
    public void addCategory(Category category) {
        categoryDao.addCategory(category);
    }
    /**
     * 修改课程分类信息
     */
    @Override
    public void updCategory(Category category) {
        categoryDao.updCategory(category);
    }
    /**
     * 删除节点及以下全部节点
     */
    @Override
    public void delCategoryByAll(CategoryVO categoryVO) {
        categoryVO.setIsDelete(1);
        categoryDao.delCategoryByOne(categoryVO.getId());
        if (!categoryVO.getChildren().isEmpty()){
            setCategoryIsDel(categoryVO.getChildren());
        }

    }
    /**
     * 多条件查询课程分类信息
     */
    @Override
    public List<Category> selCategoryByOne(Category category) {
        List<Category> categoryList = categoryDao.selCategoryByOne(category);
        return categoryList;
    }

    /**
     * 递归查找设置isDelete为1
     */
    public void setCategoryIsDel(List<CategoryVO> categoryVOList){
        for (CategoryVO categoryVO:categoryVOList){
            categoryVO.setIsDelete(1);
            categoryDao.delCategoryByOne(categoryVO.getId());
            if (!categoryVO.getChildren().isEmpty()){
                setCategoryIsDel(categoryVO.getChildren());
            }
        }
    }
    /**
     * 递归添加分类树
     */
    public List<CategoryVO> setChildrenCategoryVO(List<CategoryVO> categoryVOList, Integer parentId){
        List<CategoryVO> treeCategory =new ArrayList<>();
        for(CategoryVO categoryVO:categoryVOList){
            if(categoryVO.getPid().equals(parentId)){
                // 递归查找子菜单
                categoryVO.setChildren(setChildrenCategoryVO(categoryVOList,categoryVO.getId()));
                // 把一级菜单存放到treeMenu集合中
                treeCategory.add(categoryVO);
            }
        }
        return treeCategory;
    }


    /**
     * 首页home页面菜单
     */
    public List<CategoryDTO> getMenuTree() {
        // 获取所有一级分类
        List<CategoryDTO> rootCategories = categoryDao.findRootCategories();

        // 为每个一级分类设置子分类
        for (CategoryDTO root : rootCategories) {
            List<CategoryDTO> children = categoryDao.findByPid(root.getId());
            root.setChildren(children);
        }
        return rootCategories;
    }

}
